package Panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;


public class LabiryntPanel {

	    private boolean gameActive = true;
	    private int jumps = 0;
	    private JTextField[] ButtonArray = new JTextField[200];
	    private int[] LabFields = new int[200];
	    private Color tmpColor;
	    private ActionListener bAl = new ActionListener() {

	        @Override
	        public void actionPerformed(ActionEvent ae) {
	            try {
	                if(ae.getActionCommand().equals("loadAndPlay")){
	                    colorLab();
	                }
	                else{
	                        throw new UnknownActionCommandException();
	                }
	            } catch (UnknownActionCommandException exc) {
	                exc.printStackTrace();
	            }

	        }
	    };
	    private MouseListener mL = new MouseListener() {

	        @Override
	        public void mouseClicked(MouseEvent me) {
	            //throw new UnsupportedOperationException("Not supported yet.");
	        }

	        @Override
	        public void mousePressed(MouseEvent me) {
	            //throw new UnsupportedOperationException("Not supported yet.");
	        }

	        @Override
	        public void mouseReleased(MouseEvent me) {
	            //throw new UnsupportedOperationException("Not supported yet.");
	        }

	        @Override
	        public void mouseEntered(MouseEvent me) {
	            //System.out.println("Weszłem na " + me.getSource());
	            if (gameActive) {
	                Component c = me.getComponent();
	                JTextField tf = (JTextField) c;

	                tmpColor = tf.getBackground();
	                tf.setBackground(Color.red);
	                jumps++;
	                ResultField.setText(Integer.toString(jumps));
	                //tu trzeba obsługę wejść na kolory
	                if (tmpColor == Color.black) {
	                    JOptionPane.showMessageDialog(c, "Przegrałeś!");
	                    gameActive = false;
	                }
	                if (tmpColor == Color.green) {
	                    JOptionPane.showMessageDialog(c, "Koniec gry! Twój wynik: " + jumps);
	                    gameActive = false;
	                }
	            }
	        }

	        @Override
	        public void mouseExited(MouseEvent me) {
	            //System.out.println("Zeszłem z " + me.getSource());
	            if (gameActive) {
	                Component c = me.getComponent();
	                JTextField tf = (JTextField) c;
	                tf.setBackground(tmpColor);
	            }
	        }
	    };
	    //elementy składowe okna - panele
	    JPanel Labiryntowy;
	    JPanel Footer;
	    //przyciski
	    JButton LoadLab;
	    JLabel ResultLabel;
	    JTextField ResultField;

	    public LabiryntPanel(JPanel content) {
	    	content.removeAll();
	    	content.setLayout(new BorderLayout());
	        //definicje elementów
	        Labiryntowy = new JPanel();
	        Footer = new JPanel();
	        //poszczególne rzeczy dodajemy - labirynt
	        Labiryntowy.setLayout(new GridLayout(10, 20));
	        for (int i = 0; i < 200; i++) {
	            JTextField tempB = new JTextField();
	            tempB.addActionListener(bAl);
	            tempB.addMouseListener(mL);
	            //tempB.setText(Integer.toString(i));
	            tempB.setActionCommand(Integer.toString(i));
	            tempB.setPreferredSize(new Dimension(30, 30));
	            Labiryntowy.add(tempB);
	            ButtonArray[i] = tempB;
	        }

	        //dodajemy elementy do footera
	        LoadLab = new JButton("Wczytaj labirynt i graj od nowa!");
	        LoadLab.setActionCommand("loadAndPlay");
	        LoadLab.addActionListener(bAl);
	        ResultField = new JTextField();
	        ResultField.setPreferredSize(new Dimension(50, 25));
	        ResultLabel = new JLabel("Wynik:");

	        Footer.setLayout(new FlowLayout());
	        Footer.add(LoadLab);
	        Footer.add(new JSeparator());
	        Footer.add(ResultLabel);
	        Footer.add(ResultField);

	        //akcje końcowe
	        //wypełnienie tablicy labiryntu
	        for (int i = 0; i < 200; i++) {
	            if (i % 6 == 0) {
	                LabFields[i] = 1;
	            } else {
	                LabFields[i] = 0;
	            }
	            LabFields[1] = 2;
	            LabFields[199] = 3;
	        }
	        //pokolorowanie ekranu
	        colorLab();
	        //wstawienie zawartości końcowej
	        content.add(Labiryntowy, BorderLayout.CENTER);
	        content.add(Footer, BorderLayout.SOUTH);
	    }

	    private void colorLab() {
	        for (int i = 0; i < 200; i++) {
	            switch (LabFields[i]) {
	                case 0:
	                    ButtonArray[i].setBackground(Color.white);
	                    break;
	                case 1:
	                    ButtonArray[i].setBackground(Color.black);
	                    break;
	                case 2:
	                    ButtonArray[i].setBackground(Color.blue);
	                    break;
	                case 3:
	                    ButtonArray[i].setBackground(Color.green);
	                    break;
	            }
	        }
	        jumps = 0;
	        ResultField.setText(Integer.toString(jumps));
	        gameActive = true;
	    }

	    class UnknownActionCommandException extends Exception {
	    }

	
}
